package org.dubnation.car.service.impl;

import org.dubnation.car.domain.Car;
import org.dubnation.car.domain.CarResources;
import org.dubnation.car.mapper.CarResourcesMapper;
import org.dubnation.car.service.ICarResourcesService;
import org.dubnation.base.service.impl.BaseServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-11-02
 */
@Service
@Transactional
public class CarResourcesServiceImpl extends BaseServiceImpl<CarResources> implements ICarResourcesService {

    @Autowired(required = false)
    private CarResourcesMapper carResourcesMapper;

    @Override
    public List<CarResources> queryByCarId(Long carId) {
        return carResourcesMapper.queryByCarId(carId);
    }

    /**
     * 获取资源
     * @return
     */
    @Override
    public List<CarResources> getResources() {
        List<CarResources> carResources = carResourcesMapper.queryAll();
        for (CarResources carResource : carResources) {
            //修改车辆资源的资源类型
            Integer type = carResource.getResourceType();
            if (type == 1) carResource.setResourceTypeName(CarResources.OVERVIEW);
            if (type == 2) carResource.setResourceTypeName(CarResources.DETAIL_DRAWING);
            if (type == 3) carResource.setResourceTypeName(CarResources.TEST_REPORT);
            if (type == 4) carResource.setResourceTypeName(CarResources.VIDEO);
            if (type == 5) carResource.setResourceTypeName(CarResources.VR);
        }
        return removeDuplicate(carResources);
    }

    /**
     * 获取资源类型名称
     * @param resourceType
     * @return
     */
    @Override
    public String getResourceTypeName(Integer resourceType) {
        if (resourceType == 1) return CarResources.OVERVIEW;
        if (resourceType == 2) return CarResources.DETAIL_DRAWING;
        if (resourceType == 3) return CarResources.TEST_REPORT;
        if (resourceType == 4) return CarResources.VIDEO;
        if (resourceType == 5) return CarResources.VR;
        else return null;
    }

    /**
     * 重写新增方法
     * @param carResources
     */
    @Override
    public void insert(CarResources carResources) {
        //转换资源类型
        String resourceTypeName = carResources.getResourceTypeName();
        if (CarResources.OVERVIEW.equals(resourceTypeName)) carResources.setResourceType(1);
        if (CarResources.DETAIL_DRAWING.equals(resourceTypeName)) carResources.setResourceType(2);
        if (CarResources.TEST_REPORT.equals(resourceTypeName)) carResources.setResourceType(3);
        if (CarResources.VIDEO.equals(resourceTypeName)) carResources.setResourceType(4);
        if (CarResources.VR.equals(resourceTypeName)) carResources.setResourceType(5);
        //前端返回的资源路径是一个数组，需要转换成字符串存储
        List<String> resourceUrlList = carResources.getResourceUrlList();
        StringBuffer buffer = new StringBuffer();
        resourceUrlList.forEach(resourceUrls -> {
            if (buffer.length() > 0){
                buffer.append(",");
            }
            buffer.append(resourceUrls);
        });
        carResources.setResourceUrl(buffer.toString());
        super.insert(carResources);
    }

    /**
     * 重写修改方法
     * @param carResources
     */
    @Override
    public void update(CarResources carResources) {
        //转换资源类型
        String resourceTypeName = carResources.getResourceTypeName();
        if (CarResources.OVERVIEW.equals(resourceTypeName)) carResources.setResourceType(1);
        if (CarResources.DETAIL_DRAWING.equals(resourceTypeName)) carResources.setResourceType(2);
        if (CarResources.TEST_REPORT.equals(resourceTypeName)) carResources.setResourceType(3);
        if (CarResources.VIDEO.equals(resourceTypeName)) carResources.setResourceType(4);
        if (CarResources.VR.equals(resourceTypeName)) carResources.setResourceType(5);
        //前端返回的资源路径是一个数组，需要转换成字符串存储
        List<String> resourceUrlList = carResources.getResourceUrlList();
        StringBuffer buffer = new StringBuffer();
        resourceUrlList.forEach(resourceUrls -> {
            if (buffer.length() > 0){
                buffer.append(",");
            }
            buffer.append(resourceUrls);
        });
        carResources.setResourceUrl(buffer.toString());
        super.update(carResources);
    }



    /**
     * 去除List集合中对象的重复属性值
     * @param carResourcesList
     */
    public static List<CarResources> removeDuplicate(List<CarResources> carResourcesList) {
        if (CollectionUtils.isEmpty(carResourcesList)){
            return null;
        }
        for (int i = 0; i < carResourcesList.size(); i++) {
            CarResources carResources = carResourcesList.get(i);
            String resourceTypeName = carResources.getResourceTypeName();
            for (int j = i+1; j < carResourcesList.size() ; j++) {
                CarResources carResources1 = carResourcesList.get(j);
                String resourceTypeName1 = carResources1.getResourceTypeName();
                if (resourceTypeName == resourceTypeName1){
                    //有重复资源类型
                    carResourcesList.remove(carResources1);
                    j--;
                }
            }
        }
        return carResourcesList;
    }
}
